package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"metro-sys/models/mysql"
	"metro-sys/models/redis"
	"time"

	"metro-sys/metrorpc/internal/svc"
	"metro-sys/metrorpc/metrorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HoseDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHoseDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HoseDetailLogic {
	return &HoseDetailLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HoseDetailLogic) HoseDetail(in *metrorpc.HoseDetailRequest) (*metrorpc.HoseDetailResponse, error) {
	// todo: add your logic here and delete this line
	var hose mysql.Hose
	key := fmt.Sprintf("Detail_%d", in.HoseId)
	result, _ := redis.Rdb.Get(l.ctx, key).Result()
	if result == "" {
		err := hose.HoseGroupById(mysql.DB, in.HoseId)
		if err != nil {
			return nil, err
		}
		marshal, _ := json.Marshal(hose)
		redis.Rdb.Set(l.ctx, key, marshal, time.Minute*10)
	}

	return &metrorpc.HoseDetailResponse{
		Name:    hose.Name,
		Address: hose.Address,
	}, nil
}
